<!doctype html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="tcpdump,tcpcopy,压测,离线流量" />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />






<meta name="description" content="java进程CPU飙高是比较常见的case，排查方案网上大把，这里简单做个整理和记录。 基本思路1）jps -l找对对应进程pid 2）通过top -H -p pid找出耗用CPU消耗比较高的线程id 3）将线程id转成16进制 4）jstack pid | grep -C 30 16进制的线程id 网上大把都是这个思路，基本没啥问题，但是有几个小问题： 1.执行起来略微有点耗时，线上问题一般都比">
<meta name="keywords" content="tcpdump,tcpcopy,压测,离线流量">
<meta property="og:type" content="article">
<meta property="og:title" content="JVM进程CPU飙高排查">
<meta property="og:url" content="http://www.zhizus.com/2017-12-01-JVM进程CPU飙高排查.html">
<meta property="og:site_name" content="郑大侠的博客">
<meta property="og:description" content="java进程CPU飙高是比较常见的case，排查方案网上大把，这里简单做个整理和记录。 基本思路1）jps -l找对对应进程pid 2）通过top -H -p pid找出耗用CPU消耗比较高的线程id 3）将线程id转成16进制 4）jstack pid | grep -C 30 16进制的线程id 网上大把都是这个思路，基本没啥问题，但是有几个小问题： 1.执行起来略微有点耗时，线上问题一般都比">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-06-07T11:09:23.191Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="JVM进程CPU飙高排查">
<meta name="twitter:description" content="java进程CPU飙高是比较常见的case，排查方案网上大把，这里简单做个整理和记录。 基本思路1）jps -l找对对应进程pid 2）通过top -H -p pid找出耗用CPU消耗比较高的线程id 3）将线程id转成16进制 4）jstack pid | grep -C 30 16进制的线程id 网上大把都是这个思路，基本没啥问题，但是有几个小问题： 1.执行起来略微有点耗时，线上问题一般都比">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: "",
      labels: ""
    }
  };
</script>



  <link rel="canonical" href="http://www.zhizus.com/2017-12-01-JVM进程CPU飙高排查.html"/>





  <title> JVM进程CPU飙高排查 | 郑大侠的博客 </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?f75c9cec9ecead0dfce9a108b9688072";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>










  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">郑大侠的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-crossed">
          <a href="http://zhizus.com" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-camera-retro"></i> <br />
            
            穿越
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-case " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://www.zhizus.com/2017-12-01-JVM进程CPU飙高排查.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="DempeZheng">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="https://avatars3.githubusercontent.com/u/6263631?v=3&s=466">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="郑大侠的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                JVM进程CPU飙高排查
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            

            

            
          </span>

          

          
            
          

          
          
             <span id="/2017-12-01-JVM进程CPU飙高排查.html" class="leancloud_visitors" data-flag-title="JVM进程CPU飙高排查">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>java进程CPU飙高是比较常见的case，排查方案网上大把，这里简单做个整理和记录。</p>
<h2 id="基本思路"><a href="#基本思路" class="headerlink" title="基本思路"></a>基本思路</h2><p>1）<code>jps -l</code>找对对应进程pid
2）通过<code>top -H -p pid</code>找出耗用CPU消耗比较高的线程id
3）将线程id转成16进制
4）<code>jstack pid | grep -C 30 16进制的线程id</code>
网上大把都是这个思路，基本没啥问题，但是有几个小问题：
1.执行起来略微有点耗时，线上问题一般都比较紧急，争分夺秒，能省则省。
2.第二步到第四步有时间差，很可能真正过滤出来的并发当前CPU损耗较高的线程</p>
<p>既然这样，那么，如果有一个脚本来解决这个问题，那就再好不过。</p>
<h2 id="祖传脚本"><a href="#祖传脚本" class="headerlink" title="祖传脚本"></a>祖传脚本</h2><p>jvm_topn_thread.sh</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># write by    : oldmanpushcart@gmail.com</span></span><br><span class="line"><span class="comment"># date        : 2014-01-16</span></span><br><span class="line"><span class="comment"># version     : 0.07</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">typeset</span> top=<span class="variable">$&#123;1:-10&#125;</span></span><br><span class="line"><span class="built_in">typeset</span> pid=<span class="variable">$&#123;2:-$(pgrep -u $USER java)&#125;</span></span><br><span class="line"><span class="built_in">typeset</span> tmp_file=/tmp/java_<span class="variable">$&#123;pid&#125;</span>_$$.trace</span><br><span class="line"></span><br><span class="line"><span class="variable">$JAVA_HOME</span>/bin/jstack <span class="variable">$pid</span> &gt; <span class="variable">$tmp_file</span></span><br><span class="line">ps H -eo user,pid,ppid,tid,time,%cpu --sort=%cpu --no-headers\</span><br><span class="line">        | tail -<span class="variable">$top</span>\</span><br><span class="line">        | awk -v <span class="string">"pid=<span class="variable">$pid</span>"</span> <span class="string">'$2==pid&#123;print $4"\t"$6&#125;'</span>\</span><br><span class="line">        | <span class="keyword">while</span> <span class="built_in">read</span> line;</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">        <span class="built_in">typeset</span> nid=$(<span class="built_in">echo</span> <span class="string">"<span class="variable">$line</span>"</span>|awk <span class="string">'&#123;printf("0x%x",$1)&#125;'</span>)</span><br><span class="line">        <span class="built_in">typeset</span> cpu=$(<span class="built_in">echo</span> <span class="string">"<span class="variable">$line</span>"</span>|awk <span class="string">'&#123;print $2&#125;'</span>)</span><br><span class="line">        awk -v <span class="string">"cpu=<span class="variable">$cpu</span>"</span> <span class="string">'/nid='</span><span class="string">"<span class="variable">$nid</span>"</span><span class="string">'/,/^$/&#123;print $0"\t"(isF++?"":"cpu="cpu"%");&#125;'</span> <span class="variable">$tmp_file</span></span><br><span class="line"><span class="keyword">done</span></span><br><span class="line"></span><br><span class="line">rm -f <span class="variable">$tmp_file</span></span><br></pre></td></tr></table></figure>
<h2 id="使用方法"><a href="#使用方法" class="headerlink" title="使用方法"></a>使用方法</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sh  jvm_topn_thread.sh n pid</span><br></pre></td></tr></table></figure>
<p>例如 <code>sh jvm_topn_thread.sh 20 pid</code> 表示查看cpu消耗TOP 20个线程及相关信息。</p>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>自从有了这个脚本之后，大大节省了排查相关问题的时间。脚本确实是提高效率的利器。</p>
<h2 id="补充"><a href="#补充" class="headerlink" title="补充"></a>补充</h2><p>最近vip开源了<a href="https://github.com/vipshop/vjtools，里面有个vjtop的工具，体验了一把，比较经验。" target="_blank" rel="noopener">https://github.com/vipshop/vjtools，里面有个vjtop的工具，体验了一把，比较经验。</a></p>
<p><a href="https://github.com/vipshop/vjtools/tree/master/vjtop" target="_blank" rel="noopener">https://github.com/vipshop/vjtools/tree/master/vjtop</a></p>
<h2 id="附件-vjtop功能概览"><a href="#附件-vjtop功能概览" class="headerlink" title="附件 vjtop功能概览"></a>附件 vjtop功能概览</h2><h2 id="2-3-找出CPU最繁忙的线程"><a href="#2-3-找出CPU最繁忙的线程" class="headerlink" title="2.3 找出CPU最繁忙的线程"></a>2.3 找出CPU最繁忙的线程</h2><h3 id="2-3-1-命令参数"><a href="#2-3-1-命令参数" class="headerlink" title="2.3.1 命令参数"></a>2.3.1 命令参数</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">// 按时间区间内，线程占用的CPU排序，默认显示前10的线程，默认每10秒打印一次</span><br><span class="line">./vjtop.sh &lt;PID&gt;</span><br><span class="line"></span><br><span class="line">// 按线程从启动以来的总占用CPU来排序</span><br><span class="line">./vjtop.sh --totalcpu &lt;PID&gt;</span><br><span class="line"></span><br><span class="line">// 按时间区间内，线程占用的SYS CPU排序</span><br><span class="line">./vjtop.sh --syscpu &lt;PID&gt;</span><br><span class="line"></span><br><span class="line">// 按线程从启动以来的总SYS CPU排序</span><br><span class="line">./vjtop.sh --totalsyscpu &lt;PID&gt;</span><br></pre></td></tr></table></figure>
<h3 id="2-3-2-输出示例："><a href="#2-3-2-输出示例：" class="headerlink" title="2.3.2 输出示例："></a>2.3.2 输出示例：</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">VJTop 1.0.0 - 11:38:02, UPTIME: 3d01h</span><br><span class="line">PID: 127197, JVM: 1.7.0_79, USER: even.liang</span><br><span class="line">PROCESS:  0.99% cpu ( 0.04% of 24 core), 2491m rss,   0m swap</span><br><span class="line">IO:   24k rchar,    1k wchar,    0 read_bytes,    0 write_bytes</span><br><span class="line">THREAD:   97 active,   89 daemon,   99 peak,  461 created, CLASS: 12243 loaded, 0 unloaded</span><br><span class="line">HEAP: 160m/819m eden, 0m/102m sur, 43m/1024m old</span><br><span class="line">NON-HEAP: 55m/256m cms perm gen, 8m/96m codeCache</span><br><span class="line">OFF-HEAP: 0m/0m direct, 0m/0m map</span><br><span class="line">GC: 0/0ms ygc, 0/0ms fgc, SAFE-POINT: 6 count, 1ms time, 1ms syncTime</span><br><span class="line">THREADS-CPU:  1.01% (user= 0.31%, sys= 0.70%)</span><br><span class="line"></span><br><span class="line">   TID NAME                                                      STATE    CPU SYSCPU  TOTAL TOLSYS</span><br><span class="line">    43 metrics-mercury-metric-logger-1-thread-1             TIMED_WAIT  0.38%  0.28% 25.48%  9.13%</span><br><span class="line">   110 metrics-mercury-metric-logger-2-thread-1             TIMED_WAIT  0.38%  0.18% 25.43%  9.10%</span><br><span class="line">   496 RMI TCP Connection(365)-192.168.200.87                 RUNNABLE  0.05%  0.05%  0.00%  0.00%</span><br><span class="line">    82 Proxy-Worker-5-10                                      RUNNABLE  0.01%  0.01%  0.93%  0.30%</span><br><span class="line">   120 threadDeathWatcher-6-1                               TIMED_WAIT  0.00%  0.00%  0.26%  0.09%</span><br><span class="line">    98 Proxy-Worker-5-16                                      RUNNABLE  0.00%  0.00%  0.80%  0.26%</span><br><span class="line">    99 Proxy-Worker-5-17                                      RUNNABLE  0.00%  0.00%  0.92%  0.31%</span><br><span class="line">    63 Proxy-Worker-5-2                                       RUNNABLE  0.00%  0.00%  1.07%  0.37%</span><br><span class="line">    70 Proxy-Worker-5-5                                       RUNNABLE  0.00%  0.00%  0.78%  0.26%</span><br><span class="line">   102 Proxy-Worker-5-20                                      RUNNABLE  0.00%  0.00%  0.80%  0.27%</span><br><span class="line"></span><br><span class="line">Note: Only top 10 threads (according cpu load) are shown!</span><br><span class="line">Cost time:  46ms, CPU time:  60ms</span><br></pre></td></tr></table></figure>
<p>进程区数据解释:</p>
<ul>
<li><code>rss</code>: <code>Resident Set Size</code>, 该进程在内存中的页的数量。该数据从/proc/\&lt;pid>/status中获取, 含义与<a href="http://man7.org/linux/man-pages/man5/proc.5.html" target="_blank" rel="noopener">proc filesystem</a>中一致。</li>
<li><code>swap</code>: 被交换出去的虚存大小。该数据从/proc/\&lt;pid>/status中获取, 含义与<a href="http://man7.org/linux/man-pages/man5/proc.5.html" target="_blank" rel="noopener">proc filesystem</a>中一致。</li>
<li><code>rchar/wchar</code>: 通过系统调用的读/写的字节数。该数据从/proc/\&lt;pid>/io中获取，含义与<a href="http://man7.org/linux/man-pages/man5/proc.5.html" target="_blank" rel="noopener">proc filesystem</a>中一致。</li>
<li><code>read_bytes/write_bytes</code>: 真正达到存储层的读/写的字节数。该数据从/proc/\&lt;pid>/io中获取，含义与<a href="http://man7.org/linux/man-pages/man5/proc.5.html" target="_blank" rel="noopener">proc filesystem</a>中一致。</li>
<li><code>codeCache</code>: JIT编译的二进制代码的存放区，满后将不能编译新的代码。</li>
<li><code>direct</code>: 堆外内存，但注意新版Netty不经过JDK API所分配的堆外内存未能纪录。</li>
<li><code>SAFE-POINT</code>: PerfData开启时可用，JVM真正的停顿次数及停顿时间</li>
</ul>
<p>线程区数据解释:</p>
<ul>
<li><code>CPU</code>: 线程在打印间隔内所占的CPU百分比(按单个核计算)</li>
<li><code>SYSCPU</code>: 线程在打印间隔内所占的SYS CPU百分比(按单个核计算)</li>
<li><code>TOTAL</code>: 从进程启动到现在，线程的总CPU时间/进程的总CPU时间的百分比</li>
<li><code>TOLSYS</code>: 从进程启动到现在，线程的总SYS CPU时间/进程的总CPU时间的百分比</li>
</ul>
<p>底部数据解释:</p>
<ul>
<li><code>Cost time</code>: 本次采集数据及输出的耗时</li>
<li><code>CPU time</code>: 本次采集数据及输出的CPU时间占用</li>
</ul>
<h2 id="2-4-找出内存分配最频繁的线程"><a href="#2-4-找出内存分配最频繁的线程" class="headerlink" title="2.4 找出内存分配最频繁的线程"></a>2.4 找出内存分配最频繁的线程</h2><h3 id="2-4-1-命令参数"><a href="#2-4-1-命令参数" class="headerlink" title="2.4.1 命令参数"></a>2.4.1 命令参数</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">// 线程分配内存的速度排序，默认显示前10的线程，默认每10秒打印一次</span><br><span class="line">./vjtop.sh --memory &lt;PID&gt;</span><br><span class="line"></span><br><span class="line">// 按线程的总内存分配而不是打印间隔内的内存分配来排序</span><br><span class="line">./vjtop.sh --totalmemory &lt;PID&gt;</span><br></pre></td></tr></table></figure>
<h3 id="2-4-2-输出示例"><a href="#2-4-2-输出示例" class="headerlink" title="2.4.2 输出示例"></a>2.4.2 输出示例</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">(忽略头信息)</span><br><span class="line"> THREADS-MEMORY:   30k/s allocation rate</span><br><span class="line"></span><br><span class="line">    TID NAME                                                 STATE         MEMORY         TOTAL-ALLOCATED</span><br><span class="line">  47636 RMI TCP Connection(583)-127.0.0.1                 RUNNABLE   27k/s(88.76%)    17m( 0.00%)</span><br><span class="line">      1 main                                              RUNNABLE    2k/s( 8.44%)   370g(83.16%)</span><br><span class="line">  47845 JMX server connection timeout 47845             TIMED_WAIT   251/s( 0.80%)    21k( 0.00%)</span><br><span class="line">  46607 Worker-501                                      TIMED_WAIT    60/s( 0.19%)   934m( 0.20%)</span><br><span class="line">  46609 Worker-502                                      TIMED_WAIT    60/s( 0.19%)   822m( 0.18%)</span><br><span class="line">  46610 Worker-503                                      TIMED_WAIT    60/s( 0.19%)   737m( 0.16%)</span><br><span class="line">  46763 Worker-504                                      TIMED_WAIT    60/s( 0.19%)   696m( 0.15%)</span><br><span class="line">  46764 Worker-505                                      TIMED_WAIT    60/s( 0.19%)   743m( 0.16%)</span><br><span class="line">  47149 Worker-506                                      TIMED_WAIT    60/s( 0.19%)   288m( 0.06%)</span><br><span class="line">  46551 Worker-500                                      TIMED_WAIT    60/s( 0.19%)   757m( 0.17%)</span><br></pre></td></tr></table></figure>
<p>进程区数据解释:</p>
<ul>
<li><code>allocation rate</code>: 所有线程在打印间隔内每秒分配的内存</li>
</ul>
<p>线程区数据解释:</p>
<ul>
<li><code>STATE</code>: 该线程当前的状态</li>
<li><code>MEMORY</code>: 该线程分配内存的瞬时值，即该线程在打印间隔内每秒分配的内存空间(该线程每秒分配的内存占所有线程在该秒分配的总内存的百分比)</li>
<li><code>TOTAL-ALLOCATED</code>: 该线程分配内存的历史累计值，即从进程启动到现在，该线程分配的总内存大小，该总内存大小包括已回收的对象的内存(该线程分配的总内存大小占所有线程分配的总内存大小的百分比)。</li>
</ul>
<h2 id="2-5-公共参数"><a href="#2-5-公共参数" class="headerlink" title="2.5 公共参数"></a>2.5 公共参数</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">// 打印其他选项</span><br><span class="line">./vjtop.sh -h</span><br><span class="line"></span><br><span class="line">// 结果输出到文件</span><br><span class="line">./vjtop.sh &lt;PID&gt; &gt; /tmp/vjtop.log</span><br><span class="line"></span><br><span class="line">// 每5秒打印一次（默认10秒）</span><br><span class="line">./vjtop.sh -d 5 &lt;PID&gt;</span><br><span class="line"></span><br><span class="line">// 显示前20的线程（默认10）</span><br><span class="line">./vjtop.sh -l 20 &lt;PID&gt;</span><br><span class="line"></span><br><span class="line">// 更宽的120字节的屏幕 （默认100）</span><br><span class="line">./vjtop.sh -w 120 &lt;PID&gt; &gt; /tmp/vjtop.log</span><br><span class="line"></span><br><span class="line">// 打印20次后退出</span><br><span class="line">./vjtop.sh -n 20 &lt;PID&gt;</span><br></pre></td></tr></table></figure>
      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        
  <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
    <div>大侠劫富济贫啦！</div>
    <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
      <span>赏</span>
    </button>
    <div id="QR" style="display: none;">
      
        <div id="wechat" style="display: inline-block">
          <img id="wechat_qr" src="/images/dempe/20180527112524.jpg" alt="DempeZheng WeChat Pay"/>
        </div>
      
      
    </div>
  </div>


      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      

      
        
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017-11-19-小记JVM参数调优（初）.html" rel="next" title="记JVM参数调优">
                <i class="fa fa-chevron-left"></i> 记JVM参数调优
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017-12-06-记一次服务性能压测.html" rel="prev" title="记一次服务性能压测">
                记一次服务性能压测 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
      <div id="SOHUCS"></div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="https://avatars3.githubusercontent.com/u/6263631?v=3&s=466"
               alt="DempeZheng" />
          <p class="site-author-name" itemprop="name">DempeZheng</p>
           
              <p class="site-description motion-element" itemprop="description">請更固執，將自己謀生與擅長的事情做到最好</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/archives">
                <span class="site-state-item-count">68</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/categories/index.html">
                <span class="site-state-item-count">23</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/dempeZheng" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://music.163.com/#/user/home?id=6758793" target="_blank" title="网易云音乐">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  网易云音乐
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://www.zhihu.com/people/dempe" target="_blank" title="知乎">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  知乎
                </a>
              </span>
            
          
        </div>

        
        

        
        
          <div class="links-of-blogroll motion-element links-of-blogroll-inline">
            <div class="links-of-blogroll-title">
              <i class="fa  fa-fw fa-globe"></i>
              Links
            </div>
            <ul class="links-of-blogroll-list">
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.wolfbe.com/" title="朗度云" target="_blank">朗度云</a>
                </li>
              
            </ul>
          </div>
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#基本思路"><span class="nav-number">1.</span> <span class="nav-text">基本思路</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#祖传脚本"><span class="nav-number">2.</span> <span class="nav-text">祖传脚本</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#使用方法"><span class="nav-number">3.</span> <span class="nav-text">使用方法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#总结"><span class="nav-number">4.</span> <span class="nav-text">总结</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#补充"><span class="nav-number">5.</span> <span class="nav-text">补充</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#附件-vjtop功能概览"><span class="nav-number">6.</span> <span class="nav-text">附件 vjtop功能概览</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-3-找出CPU最繁忙的线程"><span class="nav-number">7.</span> <span class="nav-text">2.3 找出CPU最繁忙的线程</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-1-命令参数"><span class="nav-number">7.1.</span> <span class="nav-text">2.3.1 命令参数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-2-输出示例："><span class="nav-number">7.2.</span> <span class="nav-text">2.3.2 输出示例：</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-4-找出内存分配最频繁的线程"><span class="nav-number">8.</span> <span class="nav-text">2.4 找出内存分配最频繁的线程</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-4-1-命令参数"><span class="nav-number">8.1.</span> <span class="nav-text">2.4.1 命令参数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-4-2-输出示例"><span class="nav-number">8.2.</span> <span class="nav-text">2.4.2 输出示例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-5-公共参数"><span class="nav-number">9.</span> <span class="nav-text">2.5 公共参数</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">DempeZheng</span>
</div>



        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  






  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  


  




	





  





  





  






  





  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.1.js"></script>
  <script>AV.initialize("dhzMp0HdJLWMCrt04bwybWzH-gzGzoHsz", "gH6FmxJiUaVLLRXQDWpkkIVv");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  

  

</body>
</html>
